package cn.wokoba.dao.mapper;


import cn.wokoba.dao.pojo.RaffleActivityAccountDay;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

//抽奖活动账户表-日次数
@Mapper
public interface RaffleActivityAccountDayMapper extends BaseMapper<RaffleActivityAccountDay> {

    @Update("""
               update raffle_activity_account_day
                    set day_count_surplus = day_count_surplus - 1, update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId} and day = #{day} and day_count_surplus > 0\
            """)
    int updateActivityAccountDaySubtractionQuota(RaffleActivityAccountDay build);

    @Insert("""
                 insert into raffle_activity_account_day(
                    user_id, activity_id, day, day_count, day_count_surplus, create_time, update_time)
                    values
                    (#{userId}, #{activityId}, #{day}, #{dayCount}, #{dayCountSurplus}, now(), now())\
            """)
    void insertActivityAccountDay(RaffleActivityAccountDay build);

    @Update("""
                update raffle_activity_account_day
                    set day_count_surplus = day_count_surplus + #{dayCountSurplus},
                        update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId} and day = #{day}\
            """)
    void addAccountQuota(RaffleActivityAccountDay raffleActivityAccountDay);

    @Select("""
             select user_id, activity_id, day, day_count, day_count_surplus
                    from raffle_activity_account_day
                    where user_id = #{userId} and activity_id = #{activityId} and day = #{day}\
            """)
    RaffleActivityAccountDay queryActivityAccountDayByUserId(RaffleActivityAccountDay build);

}
